package com.kb.design.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.common.ApiResponse;
import com.kb.design.common.MeException;
import com.kb.design.common.ResponseObject;
import com.kb.design.dto.DesignProjectDTO;
import com.kb.design.entity.DesignProject;
import com.kb.design.enums.DesignNodeStatus;
import com.kb.design.enums.DesignStatus;
import com.kb.design.enums.ProjectStatus;
import com.kb.design.mapper.postgreSQL.DesignDeptMapper;
import com.kb.design.service.*;
import com.kb.design.vo.DesignInfoDataVO;
import com.kb.design.vo.ProjectListVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.util.*;

/**
 * (CustomUser)表控制层
 *
 * @author makejava
 * @since 2023-08-22 10:56:58
 */
@RestController
@RequestMapping("project")
public class ProjectController {


    @Resource
    private ProjectService projectService;
    @Resource
    private PostService postService;
    @Resource
    private CustomContactService contactService;
    /**
     * 分页查询
     *
     * @param
     * @return 查询结果
     */
    @PostMapping("list")
    public ApiResponse queryByPage(@RequestBody DesignProjectDTO projectDTO) {
        ResponseObject responseObject = new ResponseObject();
        Page<ProjectListVO> projectListVOPage = projectService.queryByPage(projectDTO);
        List<ProjectListVO> records = projectListVOPage.getRecords();
        for (ProjectListVO record : records) {
            record.setProjectStatusEnum(DesignStatus.getName(record.getStatus()));
            record.setDesignStatusEnum(DesignNodeStatus.getName(record.getDesignStatus()));
            if (Objects.equals(record.getProjectStatusAuxiliary(), ProjectStatus.STOP.getIndex())){
                record.setProjectStatusEnum(ProjectStatus.STOP.getName());
            }
            if (Objects.nonNull(record.getExtensionTime()) && record.getExtensionTime() > 0 || Integer.parseInt(record.getStatus()) >= 30){
                record.setExtensionTime(null);
            }
        }
        responseObject.setItems(records);
        responseObject.setTotal(projectListVOPage.getTotal());
        return ApiResponse.success(responseObject);
    }

    /**
     * 奖金查询
     *
     * @param
     * @return 查询结果
     */
    @PostMapping("queryBonusByPage")
    public ApiResponse queryBonusByPage(@RequestBody DesignProjectDTO projectDTO) {
        ResponseObject responseObject = new ResponseObject();
        Page<ProjectListVO> projectListVOPage = projectService.queryBonusByPage(projectDTO);
        List<ProjectListVO> records = projectListVOPage.getRecords();
        for (ProjectListVO record : records) {
            record.setProjectStatusEnum(DesignStatus.getName(record.getStatus()));
            record.setDesignStatusEnum(DesignNodeStatus.getName(record.getDesignStatus()));
            if (Objects.equals(record.getProjectStatusAuxiliary(), ProjectStatus.STOP.getIndex())){
                record.setProjectStatusEnum(ProjectStatus.STOP.getName());
            }
        }
        responseObject.setItems(records);
        responseObject.setTotal(projectListVOPage.getTotal());
        return ApiResponse.success(responseObject);
    }


    /**
     * 新增数据
     *
     * @param projectDTO 实体
     * @return 新增项目并启动
     */
    @PostMapping("/add")
    public ApiResponse add(@RequestBody DesignProjectDTO projectDTO) {
        projectService.insert(projectDTO);
        return ApiResponse.success("保存成功");
    }
    /**
     * 新增未下单项目
     *
     * @param projectDTO 实体
     * @return 启动项目
     */
    @PostMapping("/addNoOrderProject")
    public ApiResponse addNoOrderProject(@RequestBody DesignProjectDTO projectDTO) {
        projectService.addNoOrderProject(projectDTO);
        return ApiResponse.success("成功");
    }
    /**
     * 新增数据
     *
     * @param projectDTO 实体
     * @return 新增未下单项目
     */
    @PostMapping("/addProject")
    public ApiResponse addProject(@RequestBody DesignProjectDTO projectDTO) {
        projectService.addProject(projectDTO);
        return ApiResponse.success("保存成功");
    }
    /**
     * 未下单项目绑定订单
     * 捆绑
     * @param
     * @return
     */
    @GetMapping("projectBindingOrder")
    public ApiResponse projectBindingOrder(@RequestParam Long projectId,@RequestParam Long orderId) {
        projectService.projectBindingOrder(projectId,orderId);
        return ApiResponse.success("绑定成功");
    }

    /**
     * 查询详情
     *
     * @param
     * @return 查询详情
     */
    @GetMapping("/searchDetailsInfo")
    public ApiResponse searchDetailsInfo(Long searchId) {
        return ApiResponse.success( projectService.searchDetailsInfo(searchId));
    }
    /**
     *
     *
     * @param
     * @return 查询详情
     */
    @GetMapping("/searchTimeLine")
    public List<Map<String ,Object>> searchTimeLine(Long searchId) {

        return projectService.searchTimeLine(searchId);
    }

    /**
     * 修改订单数据
     *
     * @param
     * @
     */
    @PostMapping("/updateOrderData")
    public ApiResponse updateOrderData(@RequestBody DesignProjectDTO projectDTO) {
        projectService.updateOrderData(projectDTO);
        return ApiResponse.success("保存成功");
    }

    /**
     *  设计完成到检查节点的状态修改
     *
     * @param
     * @
     */
    @PostMapping("/updateProjectData")
    public ApiResponse updateProjectData(@RequestBody DesignProjectDTO projectDTO) {
        projectService.updateProjectData(projectDTO);
        return ApiResponse.success("修改成功");
    }


    /**
     * 交付
     *
     * @param
     * @
     */
    @PostMapping("/deliveryProject")
    public ApiResponse deliveryProject(@RequestBody DesignProject project) throws MeException {
        projectService.deliveryProject(project);
        return ApiResponse.success("交付成功");
    }

    /**
     * 查询设计信息
     *
     * @param
     * @
     */
    @PostMapping("/searchDesignInfoData")
    public List<DesignInfoDataVO> searchDesignInfoData(@RequestBody DesignProjectDTO projectDTO) {
        return projectService.searchDesignInfoData(projectDTO);
    }
    /**
     * 退回订单
     * @param
     * @return
     */
    @GetMapping("/returnOrder")
    public ApiResponse returnOrder(@RequestParam(required = false) Long orderId,
                                           @RequestParam(required = false) Long projectId,String returnStatus) {
        return ApiResponse.success(projectService.returnOrder(orderId, projectId,returnStatus));
    }
    /**
     * 终止项目
     * @param
     * @return
     */
    @GetMapping("/termination")
    public ApiResponse termination(@RequestParam(required = true) Long projectId) {
        return ApiResponse.success(projectService.termination(projectId));
    }
    /**
     * 获取客户联系人
     */
    @PostMapping("/getCustomContact")
    public ApiResponse getCustomContact(@RequestBody DesignProjectDTO projectDTO){
        return ApiResponse.success(contactService.getCustomContactByProjectId(projectDTO));
    }

    /**
     * 获取项目分类统计数
     */
    @PostMapping("/getProjectTypeCount")
    public ApiResponse getProjectTypeCount(@RequestBody DesignProjectDTO projectDTO){
        return ApiResponse.success(projectService.getProjectTypeCount(projectDTO));
    }
    /**
     * 查询未下单项目
     */
    @PostMapping("/queryNoOrderProject")
    public ApiResponse queryNoOrderProject(@RequestBody DesignProjectDTO projectDTO){
        return ApiResponse.success(projectService.queryNoOrderProject(projectDTO));
    }
    /**
     * 根据设计负责人查询已完成的项目
     */
    @PostMapping("/queryProjectByDesignLeader")
    public ApiResponse queryProjectByDesignLeader(@RequestBody DesignProjectDTO projectDTO){
        return ApiResponse.success(projectService.queryProjectByDesignLeader(projectDTO));
    }
}

